package lambda;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;

/**
 * @author 江岸
 * @version V1.0
 * @ClassName: FilterAndPredicate
 * @description: 过滤以及条件
 * @date 2020/11/2714:33
 * @since V1.0
 */
public class FilterAndPredicate {
    public static void main(String[] args) {
//        List<String> languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");
//
//        System.out.println("Languages which starts with J :");
//        Predicate<String> condition = (str)->str.startsWith("J");
//        filter(languages, condition);
//
//        System.out.println("Languages which ends with a ");
//        filter(languages, (Predicate<String>)(str)->str.endsWith("a"));
//
//
//        System.out.println("Print all languages :");
//        filter(languages, (str)->true);
//
//        System.out.println("Print no language : ");
//        filter(languages, (str)->false);
//
//        System.out.println("Print language whose length greater than 4:");
//        filter(languages, (Predicate<String>)(str)->str.length() > 4);

        filterGroup();
    }

    public static void filter(List names, Predicate condition){
        names.stream().filter(condition).forEach((str) -> {
            System.out.println(str+" ");
        });
    }

    //多个predicate组合过滤
    public static void filterGroup(){
        // 可以用and()、or()和xor()逻辑函数来合并Predicate，
        // 例如要找到所有以J开始，长度为四个字母的名字，你可以合并两个Predicate并传入
        Predicate<String> stringLength = (n) -> n.length() == 4;
        Predicate<String> stringStart = (n) -> n.startsWith("J");
        List<String> languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");
        languages.stream().filter(stringLength.and(stringStart)).forEach(System.out::println);
        //languages.stream().filter(stringLength.negate()).forEach(System.out::println);
    }
}
